<?php

namespace plugins\module_config\controller;

use app\common\lib\user_operate\ModuleOperate as MO;
use cmf\controller\PluginAdminBaseController;

class NavController extends PluginAdminBaseController
{

    public function index()
    {

        $param = $this->request->post();

        //得到模块列表

        $mo = new MO();

        $module_list = $mo->getModuleList();

        $this->assign('module_list', $module_list['data']);

        //选择的模块信息

        $module_info = '';

        if (!empty($param)) {

            $module_info = json_decode(getModuleConfig($param['module'], 'config', 'nav.json'), true);

            $module_info = $module_info['nav'];

        }

        $this->assign('module_info', $module_info);

        //模块名

        $module = '';

        if (!empty($param['module'])) {

            $module = $param['module'];

        }

        $this->assign('module', $module);

        return $this->fetch();

    }

    //nav重新排序

    public function sort()
    {

        $param = $this->request->post();

        $module_info = json_decode(getModuleConfig($param['module'], 'config', 'nav.json'), true);

        $key = $param['key'];

        $arr1 = array_replace(array_flip($key), $module_info['nav']);

        $i = 0;

        $arr2 = [];

        foreach ($arr1 as $key => $value) {

            $arr2[$i] = $value;

            $i++;

        }

        $arr3['nav'] = $arr2;

        saveModuleConfigData($param['module'], 'config', 'nav.json', $arr3);

    }

    //修改页面

    public function editPage()
    {

        $param = $this->request->get();

        $sort = $param['sort'];

        $module_info = json_decode(getModuleConfig($param['module'], 'config', 'nav.json'), true);

        $info = $module_info['nav'][$sort];

        //返回信息

        $this->assign('info', $info);

        //返回传值

        $this->assign('param', $param);

        return $this->fetch();

    }

    public function edit()
    {

        $param = $this->request->post();

        $module_info = json_decode(getModuleConfig($param['module'], 'config', 'nav.json'), true);

        $module_info['nav'][$param['sort']]['name'] = $param['name'];

        $module_info['nav'][$param['sort']]['is_show'] = $param['is_show'];

        $return = saveModuleConfigData($param['module'], 'config', 'nav.json', $module_info);

        if ($return) {

            return json(['type' => 'success', 'msg' => '修改成功']);

        } else {

            return json(['type' => 'error', 'msg' => '修改失败']);

        }

    }

}
